//
//  CS_Page_CheckBox.m
//  Pods
//
//  Created by 网新中研 on 2017/7/28.
//
//

#import "CS_Page_CheckBox.h"

#define KVO_KEY_IS_ON @"on"

@interface CS_Page_CheckBox()

@property (nonatomic, strong) UISwitch *switchView;

@property (nonatomic, copy) SwitchOnBlock switchOnHandler;

@property (nonatomic, copy) SwitchOffBlock switchOffHandler;

@property (nonatomic, strong) CS_Page_TextView *content_CS_Page_TextView;

@end

@implementation CS_Page_CheckBox

+(id)page_CheckBoxWithTitle:(NSString *)title
                  isChecked:(BOOL)isChecked
            swtichOnHandler:(SwitchOnBlock)swtichOnHandler
           switchOffHandler:(SwitchOffBlock)swtichOffHandler{
    
    CS_Page_CheckBox *page_CheckBox = [CS_Page_CheckBox new];
    page_CheckBox.title = title;
    page_CheckBox.titleLabel = [UILabel new];
    page_CheckBox.isChecked = isChecked;
    page_CheckBox.switchOnHandler = swtichOnHandler;
    [page_CheckBox.switchView setOn:isChecked];
    page_CheckBox.switchOffHandler = swtichOffHandler;
    
    return page_CheckBox;
}

-(void)refreshContent:(NSString *)content{
    [self.content_CS_Page_TextView refreshContent:content];
}

-(void)setTitle:(NSString *)title{
    _title = title;
}

-(void)switchHandler:(id)sender{
    
    if (_switchView.isOn) {
        
        self.titleLabel.font = [UIFont boldSystemFontOfSize:FONT_SIZE_1];
        self.titleLabel.textColor = COLOR_TEXT_1;
        if (self.switchOnHandler) {
            self.switchOnHandler(self);
        }
    }else{
        
        self.titleLabel.font = [UIFont boldSystemFontOfSize:FONT_SIZE_1];
        self.titleLabel.textColor = COLOR_TEXT_2;
        if (self.switchOffHandler) {
            self.switchOffHandler(self);
        }
    }
    
}

-(NSString *)value{
    return self.switchView.isOn ? @"1" : @"0";
}

#define WIDTH_SWITCH 50.0
#define HEIGHT_SWITCH 30.0

/**
 *  父视图
 *
 *  @return 添加该视图后的Y坐标高度
 */
-(CGFloat)layoutView:(UIView *)superView primaryY:(CGFloat)primaryY{
    
    
    self.switchView.frame = CGRectMake(kWindowW - WIDTH_SWITCH - CELL_EDIT_RIGHT_MARGIN, 5, WIDTH_SWITCH, HEIGHT_SWITCH);
    
    CGFloat localPrimaryY = 10;
    
    localPrimaryY = [self.content_CS_Page_TextView layoutView:self primaryY:localPrimaryY];
    
    [self addSubview:self.switchView];
    
    self.frame = CGRectMake(0, primaryY, kWindowW, localPrimaryY);
    
    [superView addSubview:self];
    
    return primaryY + localPrimaryY + 5;
}


-(UISwitch *)switchView{
    if (_switchView == nil) {
        _switchView = [UISwitch new];
        _switchView.onTintColor = COLOR_THEME;
        [_switchView addTarget:self action:@selector(switchHandler:) forControlEvents:UIControlEventValueChanged];
        
    }
    return _switchView;
}

-(CS_Page_TextView *)content_CS_Page_TextView{
    if (_content_CS_Page_TextView == nil) {
        _content_CS_Page_TextView = [CS_Page_TextView page_textViewWithFont:[UIFont boldSystemFontOfSize:FONT_SIZE_1] textColor:COLOR_TEXT_2 content:self.title textAlignment:NSTextAlignmentLeft];
        _content_CS_Page_TextView.rightMargin = CELL_EDIT_RIGHT_MARGIN + HEIGHT_SWITCH + 30;
        _content_CS_Page_TextView.leftMargin = self.leftMargin;
    }
    return _content_CS_Page_TextView;
}

-(BOOL)isChecked{
    return _switchView.isOn;
}

-(void)setIsChecked:(BOOL)isChecked{
    [self.switchView setOn:isChecked];
    
    if (isChecked) {
        self.titleLabel.font = [UIFont boldSystemFontOfSize:FONT_SIZE_1];
        self.titleLabel.textColor = COLOR_TEXT_1;
    }else{
        self.titleLabel.font = [UIFont boldSystemFontOfSize:FONT_SIZE_1];
        self.titleLabel.textColor = COLOR_TEXT_2;
    }
}

@end
